﻿
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using Models.Common;
using IotDeviceToolHepler;

using IotDeviceToolHepler.Utils;
using GatewayClient.MQ;
using UtilsBase;
using UtilsCache;
using System.Runtime.InteropServices;
using GatewayClient.Common;
using GatewayClient.Models;

namespace GatewayClient.Service
{
    public class ServiceDeviceGateWay
    {
        public void getMachineState()
        {

            Thread.Sleep(5 * 1000);
            #region

            ModelGateway modelGateWay = new ModelGateway();
            try
            {
                //取机器信息
                List<string> macList = UtilsMachineInfo.getActiveMacAddress();
                foreach (string item in macList)
                {
                    if (string.IsNullOrEmpty(modelGateWay.mac))
                    {
                        modelGateWay.mac = item;
                    }
                    else
                    {
                        modelGateWay.mac = modelGateWay.mac + "," + item;
                    }
                }
                modelGateWay.startTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                modelGateWay.versionNo = UtilAppsettingCache.getSetting("base:versionNo");
                modelGateWay.gatewayDeviceId = CommonMachineInfo.getMachineId();
                modelGateWay.systemStartCode = UtilsMachineInfo.getSystemStartCode();
                modelGateWay.gatewayCode = UtilsMachineInfo.getGateWayMachineCode();
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex);
                //new UtilsFileHelper().log_WriteMessageAppend("error", ex.ToString());
                return;
            }
            UtilsOrangePiMachine orangePiMachine = new UtilsOrangePiMachine();
            while (true)
            {
                try
                {
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                    {
                        //机器信息  如果是window 这里会报错
                        modelGateWay.localIp = UtilsMachineInfo.getLocalIP();
                        modelGateWay.cpuTemperature = orangePiMachine.getCpuTemperature();
                        modelGateWay.gpuTemperature = orangePiMachine.getGpuTemperature();
                        modelGateWay.memoryTotal = orangePiMachine.getMemoryUseStatus(0);
                        modelGateWay.memoryUse = orangePiMachine.getMemoryUseStatus(0) - orangePiMachine.getMemoryUseStatus(1);
                        modelGateWay.memoryEmpty = orangePiMachine.getMemoryUseStatus(1);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

                try
                {

                    MQReqModel<object> mQReqModel = new MQReqModel<object>();
                    mQReqModel.respEvent = MQEventEnum.Device_Gateway_IntervalLive;// "miniServerInfo";
                    mQReqModel.respCode = ResponseCode.SUCCESS;
                    //mQReqModel.gatewayDeviceId = UtilsMachineInfo.getMachineId();
                    mQReqModel.gatewayCode = UtilsMachineInfo.getGateWayMachineCode();
                    mQReqModel.data = modelGateWay;
                    mQReqModel.guid = Guid.NewGuid().ToString();
                    new ServiceMQ_Helper().sendMsg(mQReqModel);
                    //机器信息
                    // Console.WriteLine("cpuTemperature:" + modelGateWay.cpuTemperature);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                if (ModelSystemTime.systemTime == ModelSystemTime.defaultSystemTime)
                {//第一次启动
                    Thread.Sleep(2 * 1000);
                }
                else
                {
                    //循环60 * 1000
                    Thread.Sleep(55 * 1000);
                }
            }
            #endregion
        }

    }
}
